<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/9
 * Time: 11:13
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class AdminModel
{
    const TABLE = 'admin';

    /**
     * 获取账号的信息
     * @param $where
     * @return array
     */
    public function info($where)
    {
        $result =  DB::table(self::TABLE)
            ->leftJoin('project as p','p.id','=',self::TABLE.'.projectId')
            ->leftJoin('profession as pro','pro.id','=',self::TABLE.'.professionId')
            ->where(function($query)use($where){
                if (!empty($where['id'])){
                    $query->where(self::TABLE.'.id',$where['id']);
                }
                if (!empty($where['username'])){
                    $query->where(self::TABLE.'.username',$where['username']);
                }
            })
            ->select([self::TABLE.'.*','p.name as projectName','pro.name as professionName'])
            ->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function lists(array $param)
    {
        $limit = config('yucheng.limit');
        $start = 0;
        if (isset($param['length']) && !is_null($param['length'])) {
            $limit = $param['length'];
        }
        if (isset($param['page']) && !is_null($param['page'])) {
            $start = ($param['page']-1)*$limit;
        }

        return DB::table(self::TABLE)
            ->leftJoin('admin_role as ar','ar.adminId','=',self::TABLE.'.id')
            ->leftJoin('project as p','p.id','=',self::TABLE.'.projectId')
            ->leftJoin('role as r','r.id','=','ar.roleId')
            ->where(function($query) use ($param){
                if (isset($param['projectId']) && !is_null($param['projectId'])){
                    $query->where('projectId',$param['projectId']);
                }
                if (isset($param['search']) && !is_null($param['search'])) {
                    $query->where(self::TABLE.'.username', 'like', '%' . $param['search'] . '%');
                }
            })
            ->offset($start)->limit($limit)
            ->select(self::TABLE.'.id',self::TABLE.'.username',self::TABLE.'.name',self::TABLE.'.projectId',self::TABLE.'.phone',self::TABLE.'.lastLoginTime',self::TABLE.'.status',self::TABLE.'.professionId','p.name as projectName','r.name as roleName')
            ->get()->toArray();
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function countLists(array $param)
    {
        return DB::table(self::TABLE)
            ->leftJoin('admin_role as ar','ar.adminId','=',self::TABLE.'.id')
            ->leftJoin('project as p','p.id','=',self::TABLE.'.projectId')
            ->leftJoin('role as r','r.id','=','ar.roleId')
            ->where(function($query) use ($param){
                if (isset($param['projectId']) && !is_null($param['projectId'])){
                    $query->where('projectId',$param['projectId']);
                }
                if (isset($param['search']) && !is_null($param['search'])) {
                    $query->where(self::TABLE.'.username', 'like', '%' . $param['search'] . '%');
                }
            })
            ->count();
    }

    /**
     * 更新
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where ,array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * 检查是否有用户名或账号姓名重复
     * @param array $data
     * @return mixed
     */
    public function checkRepeat(array $data)
    {
        return DB::table(self::TABLE)
            ->where('username',$data['username'])
            ->orWhere('name',$data['name'])
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return string
     */
    public function addAdmin(array $data)
    {
        try{
            DB::transaction(function () use($data){
                $roleId = $data['roleId'];
                unset($data['roleId']);
                $insertId = $this->insert($data);
                $adminRoleModel = new AdminRoleModel();
                $adminRoleModel->insert(['adminId'=>$insertId,'roleId'=>$roleId]);
            });
            return true;
        }catch (\Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data){
        return DB::table(self::TABLE)->insertGetId($data);
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function selectLists(array $param)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($param){
                $query->where('status',1);
                if (!empty($param['projectId'])){
                    $query->where('projectId',$param['projectId']);
                }
                if (isset($param['search'])){
                    $query->where('name','like','%'.$param['search'].'%');
                }
            })
            ->get()->toArray();
    }
}